<template>
	<div class="container">
		<el-card shadow="hover">
			<div style="padding: 0 0 10px 0;">
				<el-input
					size="small"
					placeholder="请输入搜索内容"
					prefix-icon="el-icon-search"
					style="max-width: 180px"
					v-model="query.keywords"
				></el-input>
				<el-button
					size="small"
					:disabled="!!(auth.indexOf('system.user.query') > -1)"
					type="primary"
					class="ml10"
					@click="initTableData"
				>搜索</el-button>
				<el-button
					size="small"
					:disabled="!!(auth.indexOf('system.user.add') > -1)"
					type="danger"
					class="ml10"
					@click="onOpenAdd"
				>添加</el-button>
			</div>
			<el-table :data="tableData.data" stripe style="width: 100%">
				<el-table-column prop="id" align="center" label="ID" show-overflow-tooltip></el-table-column>
				<el-table-column prop="roleName" align="center" label="绑定角色" show-overflow-tooltip>
					<template #default="scope">
						<el-tag effect="dark" type="danger" v-if="scope.row.roleId <= 0">尚未关联</el-tag>
						<el-tag effect="dark" v-else>{{ scope.row.roleName }}</el-tag>
					</template>
				</el-table-column>
				<el-table-column prop="account" align="center" label="用户名" show-overflow-tooltip></el-table-column>
				<el-table-column prop="nickName" align="center" label="昵称" show-overflow-tooltip></el-table-column>
				<!-- <el-table-column prop="phone" align="center" label="手机号" show-overflow-tooltip></el-table-column> -->
				<el-table-column prop="sex" align="center" label="性别" show-overflow-tooltip>
					<template #default="scope">
						<el-tag effect="dark" v-if="scope.row.sex === 0">男</el-tag>
						<el-tag effect="dark" type="danger" v-if="scope.row.sex === 1">女</el-tag>
					</template>
				</el-table-column>
				
				<el-table-column label="头像" align="center" show-overflow-tooltip>
					<template #default="scope">
						<el-avatar :size="50" :src="scope.row.avatar"></el-avatar>
					</template>
				</el-table-column>
				<el-table-column prop="email" label="邮箱" show-overflow-tooltip>
					<template #default="scope">{{ scope.row.email === '' ? '暂未绑定' : scope.row.email }}</template>
				</el-table-column>
				<el-table-column label="创建时间" show-overflow-tooltip>
					<template #default="scope">{{ timeSpanformat(scope.row.createdTime) }}</template>
				</el-table-column>
				<el-table-column prop="status" align="center" label="状态" show-overflow-tooltip>
					<template #default="scope">
						<el-tag effect="dark" type="famili" v-if="scope.row.status === 0">正常</el-tag>
						<el-tag effect="dark" type="danger" v-if="scope.row.status === 1">冻结</el-tag>
					</template>
				</el-table-column>
				<el-table-column label="操作" show-overflow-tooltip width="170">
					<template #default="scope">
						<el-button
							size="mini"
							type="text"
							:disabled="!!(auth.indexOf('system.user.set_role') > -1)"
							@click="onOpenEdit(scope.row)"
						>设置角色</el-button>
						<!-- <el-button
							size="mini"
							type="text"
							v-if="scope.row.status === 1"
							:disabled="!!(auth.indexOf('system.user.set_role') > -1)"
							@click="onEnable(scope.row.id)"
						>启用</el-button>
						<el-button
							size="mini"
							type="text"
							v-if="scope.row.status === 0"
							:disabled="!!(auth.indexOf('system.user.set_role') > -1)"
							@click="onDisable(scope.row.id)"
						>禁用</el-button> -->
					</template>
				</el-table-column>
			</el-table>
			<el-pagination
				@size-change="onHandleSizeChange"
				@current-change="onHandleCurrentChange"
				class="mt15"
				:pager-count="5"
				:page-sizes="[10, 20, 30]"
				v-model:current-page="query.pageIndex"
				background
				v-model:page-size="query.pageSize"
				layout="total, sizes, prev, pager, next, jumper"
				:total="tableData.total"
			></el-pagination>
		</el-card>
		<Add @sendRefresh="initTableData" ref="addRef" />
		<Edit @sendRefresh="initTableData" ref="editRef" />
	</div>
</template>

<script lang="ts">
import { toRefs, reactive, ref, defineComponent } from 'vue';
import { getList, changeStatus } from '/@/api/platform/user/index'
import { timeSpanformat } from '/@/utils/formatTime'
import { useStore } from '/@/store';
import Add from './component/add.vue';
import Edit from './component/setRole.vue';
import { ElMessage, ElMessageBox } from 'element-plus';
export default defineComponent({
	name: 'User',
	components: { Add, Edit },
	setup() {
		const store = useStore();
		const auth = store.state.userInfos.userInfos.authPageList;
		const state: any = reactive({
			query: {
				keywords: "",
				pageIndex: 1,
				pageSize: 10,
			},
			isShowDialog: false,
			tableData: {
				data: [],
				total: 0,
				loading: false,
			},
		});
		const addRef = ref();
		const editRef = ref();
		// 初始化表格数据
		const initTableData = () => {
			getList(state.query).then((res) => {
				state.tableData.data = res.data.rows;
				state.tableData.total = res.data.totalRows;
			})
		};
		// 当前行删除
		const onRowDel = (row: object) => {
			console.log(row);
		};
		// pageSize 改变时触发 新每页条数
		const onHandleSizeChange = (val: number) => {
			state.query.pageSize = val;
			getList(state.query).then((res) => {
				state.tableData.data = res.data.rows;
				state.tableData.total = res.data.totalRows;
			})
		};
		// 分页改变 新当前页
		const onHandleCurrentChange = (val: number) => {
			state.query.pageIndex = val;
			getList(state.query).then((res) => {
				state.tableData.data = res.data.rows;
				state.tableData.total = res.data.totalRows;
			})
		};
		// 打开新增菜单弹窗
		const onOpenAdd = (row: object) => {
			addRef.value.openDialog(row);
		};
		// 打开编辑菜单弹窗
		const onOpenEdit = (row: object) => {
			editRef.value.openDialog(row);
		};
		initTableData();
		const onEnable = (id: any) => {
			ElMessageBox({
				closeOnClickModal: false,
				closeOnPressEscape: false,
				title: '提示',
				message: '确定启用此用户?',
				showCancelButton: true,
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				beforeClose: (action, instance, done) => {
					if (action === 'confirm') {
						changeStatus({ id: id, status: 0 }).then((res) => {
							ElMessage({
								message: '启用成功！',
								type: 'success',
								duration: 500
							})
							done();
							initTableData();
						})
					} else {
						done();
					}
				},
			})

		};
		const onDisable = (id: any) => {
			ElMessageBox({
				closeOnClickModal: false,
				closeOnPressEscape: false,
				title: '提示',
				message: '确定禁用此用户?',
				showCancelButton: true,
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				beforeClose: (action, instance, done) => {
					if (action === 'confirm') {
						changeStatus({ id: id, status: 1 }).then((res) => {
							ElMessage({
								message: '禁用成功！',
								type: 'success',
								duration: 500
							})
							done();
							initTableData();
						})
					} else {
						done();
					}
				}
			})
		};
		return {
			onEnable, onDisable,
			addRef,
			editRef,
			onHandleSizeChange,
			onHandleCurrentChange,
			...toRefs(state),
			timeSpanformat,
			initTableData,
			onOpenAdd,
			onOpenEdit,
			auth
		};
	},
});
</script>
<style scoped lang="scss">
.container {
	.system-user-photo {
		width: 40px;
		height: 40px;
		border-radius: 100%;
	}
}
</style>
